import heapq
import collections


class Solution(object):
    def findShortestWay(self, maze, ball, hole):
        """
        :type maze: List[List[int]]
        :type ball: List[int]
        :type hole: List[int]
        :rtype: str
        """
        m = len(maze)
        n = len(maze[0])
        edges = set()
        last_edge = collections.defaultdict(set)
        dist = [[-1 for _ in range(n)] for _ in range(m)]
        dist[ball[0]][ball[1]] = 0
        stack = []

        def find_next(point):
            x = point[0]
            y = point[1]
            if x > 0:
                index = x
                while index - 1 >= 0 and maze[index - 1][y] == 0:
                    index -= 1
                    if [index, y] == hole:
                        break
                if index != x:
                    heapq.heappush(stack, [dist[x][y] + x - index, (x, y), (index, y)])
            if x < m - 1:
                index = x
                while index + 1 < m and maze[index + 1][y] == 0:
                    index += 1
                    if [index, y] == hole:
                        break
                if index != x:
                    heapq.heappush(stack, [dist[x][y] + index - x, (x, y), (index, y)])
            if y > 0:
                index = y
                while index - 1 >= 0 and maze[x][index - 1] == 0:
                    index -= 1
                    if [x, index] == hole:
                        break
                if index != y:
                    heapq.heappush(stack, [dist[x][y] + y - index, (x, y), (x, index)])
            if y < n - 1:
                index = y
                while index + 1 < n and maze[x][index + 1] == 0:
                    index += 1
                    if [x, index] == hole:
                        break
                if index != y:
                    heapq.heappush(stack, [dist[x][y] + index - y, (x, y), (x, index)])

        find_next(ball)
        edges.add(tuple(ball))
        while stack:
            node = heapq.heappop(stack)
            if node[2] not in edges:
                last_edge[node[2]].add(node[1])
                edges.add(node[2])
                dist[node[2][0]][node[2][1]] = node[0]
                find_next(node[2])
            else:
                if dist[node[2][0]][node[2][1]] == node[0]:
                    last_edge[node[2]].add(node[1])

        self.now_max = None

        def find_path(s, point):
            if point == tuple(ball):
                if not self.now_max:
                    self.now_max = s
                else:
                    self.now_max = min(self.now_max, s)
            else:
                for next_point in last_edge[point]:
                    if next_point[0] == point[0]:
                        if next_point[1] < point[1]:
                            t = 'r'
                        else:
                            t = 'l'
                    else:
                        if next_point[0] < point[0]:
                            t = 'd'
                        else:
                            t = 'u'
                    find_path(t + s, next_point)
        find_path('', tuple(hole))
        if self.now_max:
            return self.now_max
        else:
            return 'impossible'


data = Solution()
# maze = [[0, 0, 0, 0, 0], [1, 1, 0, 0, 1], [0, 0, 0, 0, 0], [0, 1, 0, 0, 1], [0, 1, 0, 0, 0]]
# ball = [4, 3]
# hole = [0, 1]
# print(data.findShortestWay(maze, ball, hole))
# maze = [[0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [1, 1, 0, 1, 1], [0, 0, 0, 0, 0]]
# ball = [0, 4]
# hole = [4, 4]
# print(data.findShortestWay(maze, ball, hole))
# maze = [[0, 0, 0, 0, 0], [1, 1, 0, 0, 1], [0, 0, 0, 0, 0], [0, 1, 0, 0, 1], [0, 1, 0, 0, 0]]
# ball = [4, 3]
# hole = [3, 0]
# print(data.findShortestWay(maze, ball, hole))
maze = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1]]
ball = [0, 4]
hole = [3, 5]
print(data.findShortestWay(maze, ball, hole))
